package com.pzh.assignment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.assignment.common.bean.Result;
import com.pzh.assignment.common.bean.param.ClassParam;
import com.pzh.assignment.common.bean.param.CollegeParam;
import com.pzh.assignment.common.bean.vo.ClassVO;
import com.pzh.assignment.common.bean.vo.CollegeVO;
import com.pzh.assignment.common.shiro.ShiroUtils;
import com.pzh.assignment.entity.Class;
import com.pzh.assignment.entity.College;
import com.pzh.assignment.entity.Groups;
import com.pzh.assignment.entity.Major;
import com.pzh.assignment.entity.Role;
import com.pzh.assignment.service.IClassService;
import com.pzh.assignment.service.IGroupsService;
import com.pzh.assignment.service.IMajorService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pengzhihao
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    IClassService classService;
    @Autowired
    IGroupsService groupsService;
    @Autowired
    IMajorService majorService;

    @RequiresRoles(value = {"sys_admin", "admin"}, logical = Logical.OR)
    @GetMapping
    public Result listClassByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                    @RequestParam(defaultValue = "5") Integer size) {
        Page<Class> page = new Page<>(currentPage, size);
        QueryWrapper<Class> classQueryWrapper = initClassWrapper();
        IPage pageData = classService.page(page, classQueryWrapper);
        List<ClassVO> voList = ((IPage<Class>) pageData).getRecords().stream().map(ClassVO::valueOf).collect(Collectors.toList());
        pageData.setRecords(voList);
        return Result.success(pageData);
    }

    @GetMapping("/total")
    public Result totalClass() {
        QueryWrapper<Class> classQueryWrapper = initClassWrapper();
        List<Class> classes = classService.list(classQueryWrapper);
        List<ClassVO> voList = classes.stream().map(ClassVO::valueOf).collect(Collectors.toList());
        return Result.success(voList);
    }

    @RequiresRoles(value = {"sys_admin", "admin"}, logical = Logical.OR)
    @PostMapping
    public Result saveOrUpdate(@RequestBody ClassParam param) {
        Class clazz = new Class();
        clazz.setMajorId(param.getMajorId());
        clazz.setGrade(param.getGrade());
        clazz.setClassNum(param.getClassNum());
        clazz.setPeopleNum(param.getPeopleNum());
        if (param.getId() != null) {
            clazz.setId(param.getId());
        }
        classService.saveOrUpdate(clazz);
        return Result.success(null);
    }

    @RequiresRoles(value = {"sys_admin", "admin"}, logical = Logical.OR)
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        classService.removeById(id);
        return Result.success(null);
    }

    private QueryWrapper<Class> initClassWrapper(){
        Role role = ShiroUtils.getProfile().getRole();
        Integer groupId = ShiroUtils.getProfile().getGroupId();
        QueryWrapper<Class> wrapper = new QueryWrapper<>();
        switch (role.getName()) {
            case Role.SYSTEM_ADMIN:{
                wrapper.orderByDesc("create_time");
                break;
            }
            default:{
                Groups group = groupsService.getById(groupId);
                List<Major> majorList = majorService.list(new QueryWrapper<Major>().eq("college_id", group.getCollegeId()));
                List<Integer> majorIds = majorList.stream().map(major -> major.getId()).collect(Collectors.toList());
                wrapper.in("major_id", majorIds).orderByDesc("create_time");
                break;
            }
        }
        return wrapper;
    }
}
